//
//  StarPredictAndVideoListController.m
//  LiveStar
//
//  Created by 漪珊 on 2017/4/26.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "StarPredictAndVideoListController.h"
#import "SetPredictViewController.h"
#import "VideoRecordViewController.h"
#import "StarTrendTopView.h"
#import "StarPredictCell.h"
#import "StarHomeVideoCell.h"
#import "PredictListModel.h"
#import "VideoListModel.h"

@interface StarPredictAndVideoListController ()<UITableViewDelegate,UITableViewDataSource>{
    NSArray *_itemsArray;
    NSMutableArray *_buttonsArray;
    NSMutableArray *_predictArray;
    NSMutableArray *_videoArray;
}
//滑动条
@property (nonatomic, strong) UIView *sliderline;
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UITableView *predictTabelView;
@property (nonatomic, strong) UITableView *videoTableView;
@end

@implementation StarPredictAndVideoListController

#pragma mark -- 网络请求
//预播
- (void)predictListRequest {
    NSDictionary *dict = @{@"udid":User_ID,
                           @"token":TOKEN,
                           @"page":@"1"
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kStarPredictList];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            NSArray *array = responseObject[@"data"][@"list"];
            for (NSDictionary *predictDict in array) {
                PredictListModel *model = [[PredictListModel alloc] initWithDict:predictDict];
                [_predictArray addObject:model];
            }
            [self.predictTabelView reloadData];
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

//回放
- (void)videoRecordListRequest {
    NSDictionary *dict = @{@"udid":User_ID,
                           @"token":TOKEN,
                           @"page":@"1"
                           };
    NSString *urlString = [TEST_URL stringByAppendingString:kStarReplay];
    [NetworkTools postReqeustWithParams:dict UrlString:urlString successBlock:^(id responseObject) {
        if ([responseObject[@"code"] intValue] == 200) {
            NSArray *array = responseObject[@"data"][@"list"];
            for (NSDictionary *videoDict in array) {
                VideoListModel *model = [[VideoListModel alloc] initWithDict:videoDict];
                [_videoArray addObject:model];
            }
            [self.videoTableView reloadData];
        }
    } failureBlock:^(NSError *error) {
        
    } showHUD:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpUI];
    [self setNav];
    switch (self.listType) {
        case PredictListType:{
            [self SliderAction:_buttonsArray[0]];
        }
            break;
        case VideoListType:{
            [self SliderAction:_buttonsArray[1]];
        }
            break;
            
        default:
            break;
    }

}


- (void)setUpUI {
    _itemsArray = @[@"预播",@"回放"];
    _buttonsArray = [NSMutableArray arrayWithCapacity:1];
    _predictArray = [NSMutableArray arrayWithCapacity:1];
    _videoArray = [NSMutableArray arrayWithCapacity:1];
    [self creatScrollview];
    
}
- (void)setNav {
    self.navigationItem.titleView = [self creatSliderViewWithItmes:(int)_itemsArray.count];
}

- (void)creatScrollview {
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT-kNAV_HEIGHT)];
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.scrollEnabled = YES;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.contentSize = CGSizeMake(kSCREEN_WIDTH *_itemsArray.count, kSCREEN_HEIGHT-kNAV_HEIGHT);
    [self.view addSubview:self.scrollView];
    StarTrendTopView *topView = [[NSBundle mainBundle] loadNibNamed:@"StarTrendTopView" owner:self options:nil].lastObject;
    [self.scrollView addSubview:topView];
    topView.SendTrend = ^(){
        SetPredictViewController *setPredictVC = [[SetPredictViewController alloc] init];
        [self.navigationController pushViewController:setPredictVC animated:YES];
    };
    self.predictTabelView = [[UITableView alloc] init];
    self.predictTabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 63, kSCREEN_WIDTH, kSCREEN_HEIGHT-kNAV_HEIGHT-63)];
    self.predictTabelView.delegate = self;
    self.predictTabelView.dataSource = self;
    self.predictTabelView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.predictTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.predictTabelView.backgroundColor = [UIColor whiteColor];
    topView.titleLabel.text = @"发布新预告";
    [self.scrollView addSubview:self.predictTabelView];
    
    self.videoTableView = [[UITableView alloc] init];
    self.videoTableView = [[UITableView alloc] initWithFrame:CGRectMake(kSCREEN_WIDTH, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT-kNAV_HEIGHT)];
    self.videoTableView.delegate = self;
    self.videoTableView.dataSource = self;
    self.videoTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.videoTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.videoTableView.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:self.videoTableView];
}




- (UIView *)creatSliderViewWithItmes:(int)count {
    UIView *sliderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 160, 42)];
    CGFloat Width = sliderView.width/count;
    for (int i = 0; i<count; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(Width*i, 0, Width, 40)];
        btn.titleLabel.font = UIFont(18);
        [btn setTitle:_itemsArray[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(SliderAction:) forControlEvents:UIControlEventTouchUpInside];
        [sliderView addSubview:btn];
        [_buttonsArray addObject:btn];
    }
    
    self.sliderline = [[UIView alloc] initWithFrame:CGRectMake(0, 40, Width, 2)];
    self.sliderline.backgroundColor = [UIColor colorWithHexString:@"04DD98"];
    [sliderView addSubview:self.sliderline];
    return sliderView;
}

#pragma mark -- tableView 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == self.predictTabelView) {
        return _predictArray.count;
    } else {
        return _videoArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.predictTabelView) {
        StarPredictCell *cell = [StarPredictCell loadNibCellWithTabelView:tableView];
        cell.backgroundColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    StarHomeVideoCell *cell = [StarHomeVideoCell loadNibCellWithTabelView:tableView];
    VideoListModel *model = _videoArray[indexPath.row];
    cell.videoModel = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.predictTabelView) {
        
    }
    VideoListModel *model = _videoArray[indexPath.row];
    VideoRecordViewController *recordVC = [[VideoRecordViewController alloc] init];
    recordVC.channelId = model.l_channel;
    [self.navigationController pushViewController:recordVC animated:YES];
}

#pragma mark -- 区头视图

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == self.predictTabelView) {
        return 115;
    }
    return 111+kSCREEN_WIDTH;
}


//SliderAction
- (void)SliderAction:(UIButton *)sender {
    //    [UIView animateWithDuration:0.3 animations:^{
    //        self.sliderline.centerX = sender.centerX;
    //        [self.sliderline layoutIfNeeded];
    //    }];
    if (sender == _buttonsArray[0]) {//预播
        [self predictListRequest];
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    } else {//回放
        [self videoRecordListRequest];
        [self.scrollView setContentOffset:CGPointMake(kSCREEN_WIDTH, 0) animated:YES];
        
    }
}

#pragma mark -- scrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSUInteger index = self.scrollView.contentOffset.x / kSCREEN_WIDTH;
    UIButton *btn =  _buttonsArray[index];
    [UIView animateWithDuration:0.3 animations:^{
        self.sliderline.centerX = btn.centerX;
        [self.sliderline layoutIfNeeded];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
